<template>
	<view class="poAbs wPer100 hPer100 backCol0a0a0aS5 zIndex1 top0 left0 foWeiBold" @touchmove.stop.prevent>
		<view class="poFixed top50P left50P w600r backColWhite h500r borRad30r trTrn50Pn50P">
			<image :src="baseUrl + 'iconCloseWhite.png'" mode="aspectFit"
				class="w70r h70r poAbs left50P trTrXn50P bomn100r" @click="closeRightOff"
			></image>
			<view class="teAliCenter mt60r f32r">
				消费时长
			</view>
			<view class="teAliCenter mt70r disFlex aliItCenter juConCenter">
				<button @click="cliMinusSign" :disabled="hourVal <= rightOffObj.hourMin"
					:class="['notBtnSty', 'w100r', 'h100r', 'lh100r', 'disInBlo', 'borRad10r',
						'teAliCenter', 'f50r', 'mar0',
						hourVal <= rightOffObj.hourMin ? 'backColF7f8f9 colC8c9cb' : 'backColF2f3f4'
					 ]"
				>
					-
				</button>
				<view class="w100r h100r lh100r disInBlo borRad10r backColF2f3f4 teAliCenter ml10r mr10r">
					{{hourVal}}
				</view>
				<button @click="cliPlusSign" :disabled="hourVal >= rightOffObj.hourMax"
					:class="['notBtnSty', 'w100r', 'h100r', 'lh100r', 'disInBlo', 'borRad10r',
						'teAliCenter', 'f50r', 'mar0',
						hourVal >= rightOffObj.hourMax ? 'backColF7f8f9 colC8c9cb' : 'backColF2f3f4'
					 ]"
				>
					+
				</button>
				<view class="w100r h100r lh100r disInBlo teAliCenter ml10r">
					小时
				</view>
			</view>
			<view class="teAliCenter f30r colFff backImgLiGrTR2c6739a99c043 w200r lh80r borRad40r mAuto mt70r foWeiNor"
				@click="cliAppointmentTime1"
			>
				下一步
			</view>
		</view>
	</view>
</template>
<script>
import { baseUrl } from '@/utils/request.js'
import { unifiedOrder, getMemberCardruleInfo,
	getTaeRoomDataInfo, isTimeCanMake } from '@/utils/api.js'
import { dateLimit } from '@/utils/utils.js'
export default {
	props: {
		rightOffObj: {
			type: Object,
			default: {
				topIsWho: { type: Number, default: 0},//上级页面 1为列表 2为房间
				hourMin: { type: Number, default: 0 },//起订时长
				hourMax: { type: Number, default: 0 },//最大可订时长
				tid: { type: String, default: '' },//茶室id
				rid: { type: String, default: '' },//房间id
				setMealType: { type: String, default: '2' },//是否必购套餐 1是 2否
				setMealId: { type: String, default: '' },//sid传套餐id
				setMealMoney: { type: Number, default: 0 },//选择的套餐价格
				cardDiscount: { type: String, default: '' },//会员卡折扣
				originalCost: { type: String, default: '' },//原价/每小时
			}
		}
	},
	data() {
		return {
			baseUrl,
			hourVal: 1,//要使用的小时数
		}
	},
	mounted() {
		// console.log(new Date().getHours() + ':' + new Date().getMinutes())
		if(this.rightOffObj.topIsWho == 2) {
			this.renovateRight();
		}
	},
	methods: {
		renovateRight() {
			this.rightOffObj.hourMin = Number(this.rightOffObj.hourMin);
			this.hourVal = Number(this.rightOffObj.hourMin);
		},
		closeRightOff() {
			this.$emit('closeRightOff')
		},
		cliMinusSign() {
			if(this.hourVal > this.rightOffObj.hourMin) {
				this.hourVal -= 0.5;
			}
		},
		cliPlusSign() {
			if(this.hourVal < this.rightOffObj.hourMax) {
				this.hourVal += 0.5;
			}
		},
		cliAppointmentTime1() {
			if(this.rightOffObj.setMealType == '1' && this.rightOffObj.setMealId == '') {
				return uni.showToast({
					title: '必购套餐，请选择套餐',
					icon: 'none', duration: 3000
				})
			}
			isTimeCanMake({
				token: uni.getStorageSync('token'),
				tid: this.rightOffObj.tid,
				rid: this.rightOffObj.rid,
				hour: this.hourVal
			}).then(res1 => {
				let start_time = dateLimit() + ' ' + (new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()) + ':' + (new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()),
					startTxt = start_time.replace(/T/g,' ').replace(/-/g,"/").indexOf('.')>-1?start_time.replace(/T/g,' ').replace(/-/g,"/").substring(0,start_time.replace(/T/g,' ').replace(/-/g,"/").indexOf('.')):start_time.replace(/T/g,' ').replace(/-/g,"/"),
					addThreeHour = new Date(new Date(startTxt).getTime() + this.hourVal * 60 * 60 * 1000),
					end_time = dateLimit('', addThreeHour) + ' ' + (new Date(addThreeHour).getHours() < 10 ? '0' + new Date(addThreeHour).getHours() : new Date(addThreeHour).getHours()) + ':' + (new Date(addThreeHour).getMinutes() < 10 ? '0' + new Date(addThreeHour).getMinutes() : new Date(addThreeHour).getMinutes()),
				    item = {
						tid: this.rightOffObj.tid,
						rid: this.rightOffObj.rid,
						start_time,
						end_time,
						hourVal: this.hourVal,
						setMealId: this.rightOffObj.setMealId,
						setMealMoney: this.rightOffObj.setMealMoney,
						order_type: 2
					}
				// console.log('start_time', start_time)
				// console.log('addThreeHour', addThreeHour)
				// console.log('end_time', end_time)
						// date = new Date(data);
				// console.log('111', new Date(new Date(startTxt).getTime() + this.hourVal * 60 * 60 * 1000))
				// uni.showModal({
				// 	title: '1', content: start_time + '蛤' + new Date(start_time).getTime() + '蛤'
				// })
				uni.navigateTo({
					url: '/pages/index/orderSettlement?item=' + JSON.stringify(item)
				})
			})
			return;
			if(this.rightOffObj.setMealType == '1' && this.rightOffObj.setMealId == '') {
				return uni.showToast({
					title: '必购套餐，请选择套餐',
					icon: 'none', duration: 3000
				})
			}
			isTimeCanMake({
				token: uni.getStorageSync('token'),
				tid: this.rightOffObj.tid,
				rid: this.rightOffObj.rid,
				hour: this.hourVal
			}).then(res1 => {
				uni.showActionSheet({
					itemList: ['余额支付', '微信支付'],
					success:res2 => {
						getMemberCardruleInfo({
							token: uni.getStorageSync('token')
						}).then(res3 => {
							if(res2.tapIndex == 0) {
								let orderMoney = 0;
								if(res3.data && res3.data.type == 1) {
									orderMoney = Math.round((Math.round((this.hourVal) * 100) / 100 * Number(this.rightOffObj.originalCost) * Number(this.rightOffObj.cardDiscount)) * 100) / 100 + Number(this.rightOffObj.setMealMoney);
								}else {
									orderMoney = Math.round((Math.round((this.hourVal) * 100) / 100 * Number(this.rightOffObj.originalCost)) * 100) / 100 + Number(this.rightOffObj.setMealMoney);
								}
								uni.showModal({
									title: '提示', content: `确定余额支付${orderMoney}元？`, confirmColor: '#99c043',
									success:res4 => {
										if (res4.confirm) {
											this.cliAppointmentTime2(1, (res3.data && res3.data.type == 1 ? res3.data.id : ''));
										}
									}
								})
							}else if(res2.tapIndex == 1) {
								this.cliAppointmentTime2(2, (res3.data && res3.data.type == 1 ? res3.data.id : ''));
							}
						})
					}
				})
			})
		},
		cliAppointmentTime2(payType, card_id) {
			let start_time = dateLimit() + ' ' + (new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()) + ':' + (new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()),
				addThreeHour = new Date(new Date(start_time).getTime() + this.hourVal * 60 * 60 * 1000),
				end_time = dateLimit('', addThreeHour) + ' ' + (new Date(addThreeHour).getHours() < 10 ? '0' + new Date(addThreeHour).getHours() : new Date(addThreeHour).getHours()) + ':' + (new Date(addThreeHour).getMinutes() < 10 ? '0' + new Date(addThreeHour).getMinutes() : new Date(addThreeHour).getMinutes());
			// console.log(start_time)
			// console.log(end_time)
			// return;
			unifiedOrder({
				token: uni.getStorageSync('token'),
				tid: this.rightOffObj.tid,
				rid: this.rightOffObj.rid,
				payType,
				start_time,
				end_time,
				order_type: 2,
				order_status: 1,
				sid: this.rightOffObj.setMealId,
				card_id
			}).then(res2 => {
				if(payType == 1) {
					let title = '',
						content = '';
					if(res2.code == '000000') {
						title = '预订成功';
						content = '订单号：' + res2.data.out_trade_no;
						this.cliAppointmentTime3(title, content);
						this.cliAppointmentTime4();
					}else {
						title = '预订失败';
						if(res2.code == '900001' || res2.code == '900002') {
							if(res2.code == '900001') content = '未查询到该店铺余额信息';
							if(res2.code == '900002') content = '余额支付余额不足';
							uni.showModal({
								title, content, cancelText: '我知道了',
								confirmText: '去充值', confirmColor: '#99c043',
								success:res3 => {
									if (res3.confirm) {
										uni.navigateTo({ url: '/pages/index/recharge' })
									} else if (res3.cancel) {
										this.closeRightOff();
									}
								}
							})							
						} else {
							content = res2.message;
							this.cliAppointmentTime3(title, content);
						}
					}
				}else if(payType == 2) {
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res2.data.timeStamp,
						nonceStr: res2.data.nonceStr,
						package: res2.data.package,
						signType: res2.data.signType,
						paySign: res2.data.paySign,
						success:res3 => {
							console.log('支付成功', res3);
							this.cliAppointmentTime3('预订成功', '支付成功');
							this.cliAppointmentTime4();
						}, fail:err3 => {
							console.log('支付失败-err3', err3);
							this.cliAppointmentTime3('预订失败', '您已取消支付');
						}
					})
				}
			})
		},
		cliAppointmentTime3(title, content) {
			uni.showModal({
				title, content, confirmColor: '#99c043', showCancel: false, confirmText: '我知道了',
				success:res1 => {
					if (res1.confirm) {
						// this.subscribeFun();
						this.closeRightOff();
						if(this.rightOffObj.topIsWho == 2) this.$emit('renovateColor');
					}
				}
			})
		},
		cliAppointmentTime4() {
			getTaeRoomDataInfo({
				tid: this.rightOffObj.tid,
				rid: this.rightOffObj.rid
			}).then(res => {
				let historyRoomList = uni.getStorageSync('historyRoomList');
				// console.log(11, historyRoomList);
				if(historyRoomList == '') historyRoomList = [];
				historyRoomList.push(res.data);
				uni.setStorageSync('historyRoomList', historyRoomList);
				// console.log(22, historyRoomList);
			})
		},
		subscribeFun() {
			// uni.requestSubscribeMessage({
			//   tmplIds: [
			// 	  'yQ8VFslxofDFsztSLAEAhz1by8KftlqXeewH6a7ENbE',//订单付款通知
			// 	  '3A5LLFNlLOI8FmuQg1VoTSQ2Z-sgMbdhENmlyfd15FM',//服务完成通知
			// 	  'B8AviMN-18noH7ULHMevdxfu08liKx9vjG16v-LJHEE'//订单即将到期提醒
			//   ],
			//   success:res1 => {
			// 	  console.log('res1', res1)
			// 	  // {
			// 		 //  EbBMjRz1108yYoetYXr8bF22iu6T_RzFEk91VuWNql8: "accept",
			// 		 //  errMsg: "requestSubscribeMessage:ok"
			// 	  // }
			//   }, fail:err1 => {
			// 	  console.log('err1', err1)
			//   }
			// })
			uni.showModal({
				title: '提示', content: '是否立即跳转订单列表一键使用？', confirmColor: '#99c043',
				success:res1 => {
					if (res1.confirm) {
						uni.switchTab({ url: '/pages/order/index' })
					}
				}
			})
		}
	}
}
</script>